package com.hisoft.controller;

import com.hisoft.annotation.PassToken;
import com.hisoft.annotation.UserLoginToken;
import com.hisoft.mapper.UserMapper;
import com.hisoft.pojo.User;
import com.hisoft.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jwt_token_demo01
 * @description:
 * @author: wlg
 * @create: 2021-08-13 14:40:33
 **/
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PassToken
    @RequestMapping("/login")
    public Object login(@RequestParam("userCode")String userCode,
                        @RequestParam("userPassword")String userPassword){
        User user = userMapper.selectUserByUserCode(userCode);
        Map map = new HashMap();
        if(user.getUserPassword().equals(userPassword)){
            map.put("status",1);
            map.put("msg","success");
            map.put("token", JWTUtils.getToken(userCode,userPassword));
        }else{
            map.put("status",0);
            map.put("msg","error");
        }
        return map;
    }

    @UserLoginToken
    @RequestMapping("/hello")
    public Object hello(){
        Map map = new HashMap();
        map.put("name","郭靖");
        map.put("birthday",new Date());
        List<User> userList = userMapper.getUserList();
        map.put("userlist",userList);
        return map;
    }


}
